package com.link.base.base.questionsurvey.controller;

import com.link.base.base.questionnaire.model.QuesAns;
import com.link.base.base.questionnaire.service.QuesAnsService;
import com.link.base.base.questionsurvey.model.Question;
import com.link.base.base.questionsurvey.service.QuestionService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.service.BasicServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 问卷操作服务
 */
@Controller
@RequestMapping("/questionsurvey")
public class QuestionViewController {

    @Resource
    private QuestionService questionService;

    @Resource
    private QuesAnsService quesAnsService;

    @RequestMapping(value = "/questionDetail/{id}")
    @ResponseBody
    public ModelAndView queryQuestionDetailId(@PathVariable String id) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("question/questionDetail");
        try {
            Question question = questionService.queOneQuestionDetailInfo(Long.parseLong(id));
            mav.addObject("question", question);
        } catch (Exception e) {
            LCLogger.withException(e);
        }
        return mav;
    }

    @RequestMapping(value = "/userAnswerInfo")
    @ResponseBody
    public ModelAndView userAnswerInfo(String questionId, String userId) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("question/userAnswerInfo");
        try {
            Question question = questionService.queOneQuestionDetailInfo(Long.parseLong(questionId));
            mav.addObject("question", question);
            mav.addObject("userId", userId);
        } catch (Exception e) {
            LCLogger.withException(e);
        }
        return mav;
    }

    @RequestMapping(value = "/questionResultDetail/{id}")
    @ResponseBody
    public Map<String, Object> questionResultDetail(@PathVariable String id) {
        Map<String, Object> result = new HashMap<>();

        try {
            Question question = questionService.queOneQuestionDetailInfo(Long.parseLong(id));
            //mav.addObject("question", question);
            result.put("result", question);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("result", e.getMessage());
            result.put("success", false);
        }
        return result;
    }

    /**
     * 查询一条问卷详情
     */
    @RequestMapping(value = "/queOneQuestionDetailInfo")
    @ResponseBody
    public Map<String, Object> queOneQuestionDetailInfo(Question entity, HttpSession session,
                                                        HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Long id = entity.getId();
            if (null == id) {
                throw new BasicControllerException("ID不能为空");
            }
            Question record = questionService.queOneQuestionDetailInfo(id);
            result.put("result", record);
            result.put("success", true);
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/dzr/questionDetail/{headId}/{userId}")
    @ResponseBody
    public ModelAndView queryDzrQuestionDetailId(@PathVariable String headId, @PathVariable String userId) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("question/dzr/questionDetail");
        try {
            Question question = questionService.queOneQuestionDetailInfo(Long.parseLong(headId));
            QuesAns qa = new QuesAns();
            qa.setQuestionId(Long.parseLong(headId));
            qa.setEmpId(Long.parseLong(userId));
            QuesAns quesAns = quesAnsService.getQuesAnsByQuesIdAndUserId(qa);
            mav.addObject("question", question);
            mav.addObject("quesAns", quesAns);
        } catch (Exception e) {
            LCLogger.withException(e);
        }
        return mav;
    }
}
